<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2018/11/20
 * Time: 16:48
 */
namespace common\sources\read\taobao;

use common\models\craftsman\TaoBao;
use common\models\craftsman\BuyUser;
use yii\data\Pagination;

class TaoBaoAccess implements  ITaoBaoAccess
{
    public function getTaoBaoById($bId){
        return TaoBao::find()
                ->select("id,tbname,sex,FROM_UNIXTIME(create_time) as create_time,takename,zfbage,takephone,pro_name,pro_code,city_name,city_code,area_name,area_code,addr,is_display,access_task_num")
                ->where(['buy_id' => $bId])
                ->asArray()
                ->all();
    }

    public function getTaoBaoByTid($tid){
        return TaoBao::find()
            ->select("id,tbname,sex,create_time,takename,takephone,pro_name,pro_code,city_name,city_code,area_name,area_code,addr,is_display,access_task_num")
            ->where(['id' => $tid])
            ->asArray()
            ->one();
    }

    public function getTaoBaoByTbname($tbname) {
        return TaoBao::find()
            ->select("id,buy_id")
            ->where(['tbname' => $tbname])
            ->asArray()
            ->one();
    }


	/**
	 * newGetTaoBaoList
	 * @param $data
	 * @author 中崋 (1291713293@qq.com)
	 * @date   2019/5/9
	 * @return mixed
	 */
	public function     getTaoBaoGroupBy($where, $data) {

		$query = TaoBao::find()->alias("t")
			->select("t.id,t.buy_id,u.phone")
			->leftJoin("buyuser u", "t.buy_id = u.id")
			->groupBy('buy_id')
			->where($where);

		$result['count'] = $query->count();
		if($result['count'] >0){
			$tatol_page = ceil($result['count'] /$data['limit']);
			if($tatol_page<$data['page_num']){
				$data['page_num'] = 1;
			}
		}

		$result['list']  = $query->offset(($data['page_num'] - 1) * $data['limit'])->limit($data['limit'])->asArray()->all();
		$result['page_num'] = $data['page_num'];
		return $result;
	}

	/**
	 * getBuyTaoBaoList 获取买手买号列表
	 * @param $data
	 * @author 中崋 (1291713293@qq.com)
	 * @date   2019/5/9
	 * @return array|\yii\db\ActiveRecord[]
	 */
	public function getBuyTaoBaoList($data) {
		$field  = 'create_time,id,buy_id,tbname,sex,zfbname,zfbage,takename,takephone,pro_name,pro_code,area_name,city_name,city_code,addr,
		zfbname_img,ip_img,tqz_img,takeaddr,zfbhb_img,audit_status,reason,is_display,access_task_num,freeze,freeze_reason,freeze_time,auditUser,
		freezeUser,remark,remarkUser,audit_time,audit_operator,is_receive_task,thaw_time';
		$result = TaoBao::find()->where($data)->select($field)->asArray()->all();
		return $result;
	}

	//
	public function getTaoBaoList($data) {
		$where = $this->getTaoBaoWhere($data);
		$query = TaoBao::find()
			->alias("t")
			->select("t.id,t.buy_id,t.tbname,t.sex,t.create_time,
            t.takename,t.zfbname,t.zfbage,t.takephone,t.pro_name,t.pro_code,t.city_name,t.city_code,
            t.area_name,t.area_code,t.addr,t.zfbname_img,t.ip_img,t.tqz_img,takeaddr,t.is_display,
            t.zfbhb_img,t.audit_status,t.reason,t.access_task_num,
            t.freeze,t.freeze_reason,t.auditUser,t.freezeUser,t.remark,t.remarkUser,t.freeze_time");
		if (1 == $data['search_type'] && !empty($data['key'])) {
			$query->leftJoin("buyuser u", "t.buy_id = u.id");
		}

		// 使用总数来创建一个分页对象
		//$page            = new Pagination(['totalCount' => $query->count(), 'pageSize' => $data['limit']]);
		//$result['list']  = $query->where($where)->offset($page->offset)->limit($page->limit)->asArray()->all();
		//$result['count'] = $page->totalCount;
		$result['count'] = $query->where($where)->count();
		$result = $query->where($where)->offset(($data['page_num'] - 1) * $data['limit'])
			->limit($data['limit'])
			->asArray()
			->all();
		return $result;
	}

	// 弃用
    public function countTaoBaoList($data) {
        $where = $this->getTaoBaoWhere($data);
        $query = TaoBao::find()
            ->alias("t");
        if(1 == $data['search_type'] && !empty($data['key']))
        {
            $query->leftJoin("buyuser u","t.buy_id = u.id");
        }
        $result = $query->where($where)
            ->count();
        return $result;
    }
	// 弃用
	private function getTaoBaoWhere($data) {
		$sql = " 1 = 1";
		if (1 == $data['search_type'] && !empty($data['key'])) {
			$sql .= " AND u.phone = " . $data['key'];
		} else if (2 == $data['search_type'] && !empty($data['key'])) {
			$sql .= " AND t.tbname = '" . $data['key'] . "'";
		} else if (3 == $data['search_type'] && !empty($data['key'])) {
			$sql .= " AND  takename = '" . $data['key'] . "'";
		} else if (4 == $data['search_type'] && !empty($data['key'])) {
			$sql .= " AND  takephone = '" . $data['key'] . "'";
		} else if (5 == $data['search_type'] && !empty($data['key'])) {
			$sql .= " AND  addr = '" . $data['key'] . "'";
		} else if (6 == $data['search_type'] && !empty($data['key'])) {
			$sql .= " AND  zfbname = '" . $data['key'] . "'";
		}
		return $sql;
	}

    public function AuditDataPass($data) {
        $query = TaoBao::find()
            ->where(['audit_status' => 2])
            ->andWhere(['audit_operator' => $data['worker']]);
        if (!empty($data['begin_time'])) {
            $query->andWhere([
                '>',
                'audit_time',
                $data['begin_time']
            ]);
        }
        if (!empty($data['end_time'])) {
            $query->andWhere([
                '>',
                'audit_time',
                $data['begin_time']
            ]);
        }
        $num = $query->count();
        return $num;
    }

    public function AuditDataRefus($data) {
        $query = TaoBao::find()
            ->where(['audit_status' => 3])
            ->andWhere(['audit_operator' => $data['worker']]);
        if (!empty($data['begin_time'])) {
            $query->andWhere([
                '>',
                'audit_time',
                $data['begin_time']
            ]);
        }
        if (!empty($data['end_time'])) {
            $query->andWhere([
                '>',
                'audit_time',
                $data['begin_time']
            ]);
        }
        $num = $query->count();
        return $num;
    }

    //查看 手机号是否绑定其他收货地址
    public function chkPhoneIs($data){
	    return TaoBao::find()->where(' id !=' . $data['t_id'] . ' and  takephone = "' . $data['takephone'].'"')->one();
    }

    public function getTaoBaoAuditCount($data)
    {
        return TaoBao::find()
                ->where($data)
                ->count();
    }

}